package com.john.pplay.activity;

import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;

import static android.support.v4.content.PermissionChecker.PERMISSION_DENIED;

public class BaseActivity extends AppCompatActivity {
    private final int REQUEST_CODE = 666;

    @Override
    protected void onCreate(Bundle bundle){
        super.onCreate(bundle);
        checkPermission();
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults){
        switch (requestCode){
            case REQUEST_CODE:
                for (int i : grantResults){
                    if (i == PERMISSION_DENIED)
//                        checkPermission();
                        finish();
                }
                break;
        }
        super.onRequestPermissionsResult(requestCode,permissions,grantResults);
    }

    private void checkPermission(){
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M){
            return;
        }
        if (PERMISSION_DENIED == ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)){
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    REQUEST_CODE);
        }
    }

}
